--[[
    上海宝钢 10.46.101.12:8888 纯XML报文
]]
require("apm_core")
require("ApmXml")

local G_DEBUG = true

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

local KRED='\x1B[0;31m'
local KSALMON='\x1B[1;31m'
local KGREEN='\x1B[0;32m'
local KLIMEGREEN='\x1B[1;32m'
local KYELLOW='\x1B[0;33m'
local KORANGE='\x1B[0;34m'
local KBLUE='\x1B[1;34m'
local KMAGENTA='\x1B[0;35m'
local KPINK='\x1B[1;35m'
local KCYAN='\x1B[0;36m'
local RESET='\x1B[0m'

local t_xml_tree
local s_transtype
local s_transtype_name
local s_flowno
local s_rspcode
local s_rspmsg

local dict_transtype = {}

local function parseIp(ip)
    local _, _, value = string.find(ip, "(%d+.%d+)$")
    return value == nil and "" or value
end

local function protocol_detect(s_payload)
    if s_payload == nil then return false end

    t_xml_tree = ApmXml.load(s_payload, "UTF-8")

    return ApmXml.getValue(t_xml_tree, "/TX") ~= nil
end

local function parse_req()
    -- 交易类型
    s_transtype = ApmXml.getValue(t_xml_tree, "/TX/TX_CODE/text()")
    if s_transtype ~= nil then
        s_transtype = string.trim(s_transtype)
        s_transtype_name = dict_transtype[s_transtype]
        if s_transtype_name == nil then s_transtype_name = s_transtype end
    end

    -- 流水号
    s_flowno = ApmXml.getValue(t_xml_tree, "/TX/REQUEST_SN/text()")

    return true
end

local function parse_resp()
    -- 返回码
    s_rspcode = ApmXml.getValue(t_xml_tree, "/TX/RETURN_CODE/text()")

    -- 返回信息
    s_rspmsg = ApmXml.getValue(t_xml_tree, "/TX/RETURN_MSG/text()")

    return true
end

--- main
function post_analysis()
    local ApmData = require("ApmData")
    if ApmData.getL7Proto() ~= 50003 then return end
    if ApmData.getMsgType() ~= 1 then return end

    local s_req
    local s_resp

    s_req = ApmData.getField("request.xml.message", 1)
    if s_req ~= nil then
        s_req = string.trim(s_req)
        if protocol_detect(s_req) then
            parse_req()
        end
    end

    s_resp = ApmData.getField("response.xml.message", 1)
    if s_resp ~= nil then
        s_resp = string.trim(s_resp)
        if protocol_detect(s_resp) then
            parse_resp()
        end
    end

    if s_transtype == nil then
        logInfo("s_transtype is nil REQ:["..KRED.."%s"..RESET.."]", s_req)
    elseif s_rspcode == nil then
        logInfo("s_rspcode is nil RESP:["..KBLUE.."%s"..RESET.."]", s_resp)
    else
        ApmData.replaceField("transaction.type_code", s_transtype)
        ApmData.replaceField("transaction.type_name", s_transtype_name)
        ApmData.replaceField("transaction.title", s_transtype_name)

        ApmData.replaceField("response.code_string", s_rspcode)
        ApmData.delFields("response.code")

        if s_rspmsg ~= nil then
            ApmData.replaceField("response.error_message", s_rspmsg)
        end

        if s_flowno ~= nil then
            ApmData.replaceField("transaction.code", s_flowno)
        end

        logInfo("(%s:%d->%s:%d) name:%s|seq:%s|ret:%s|%s",
            ApmData.getSrcIp(),
            ApmData.getSrcPort(),
            parseIp(ApmData.getDstIp()),
            ApmData.getDstPort(),
            s_transtype_name,
            s_flowno,
            s_rspcode,
            s_rspmsg
        )
    end

    return
end
--101.12:8888
--v1.0.0
